﻿using System;
using Microsoft.VisualStudio.Package;
using Microsoft.VisualStudio.TextManager.Interop;
using Microsoft.WowAddonStudio.Lua.AST;
using Microsoft.WowAddonStudio.Lua.Model;
using Microsoft.WowAddonStudio.Lua.Parser;

namespace Microsoft.WowAddonStudio.Lua.LanguageService.Model
{
    public class LuaParseRequest : ParseRequest
    {
        public LuaParseRequest(int line, int column, TokenInfo tokenInfo, string source, string filename, ParseReason reason, IVsTextView view, AuthoringSink sink, bool synchronous)
            : base(line, column, tokenInfo, source, filename, reason, view, sink, synchronous)
        {
        }

        /// <summary>
        /// Gets or sets the <see cref="Chunk"/> parsed as part of this request.
        /// </summary>
        public Chunk Chunk { get; set; }

        /// <summary>
        /// Gets a value indicating whether the parse was successful.
        /// </summary>
        public bool Succeeded
        {
            get { return Chunk != null; }
        }

        /// <summary>
        /// Gets a value indicating whether a <see cref="QualifiedName"/> is available for the this.
        /// </summary>
        public bool HasQualifiedName
        {
            get { return QualifiedName != null; }
        }

        /// <summary>
        /// Gets the <see cref="QualifiedName"/> used to fulfill the request.
        /// </summary>
        public QualifiedName QualifiedName
        {
            get
            {
                if (this.Reason == ParseReason.Goto ||
                    this.Reason == ParseReason.QuickInfo ||
                    this.Reason == ParseReason.CompleteWord ||
                    this.Reason == ParseReason.DisplayMemberList)
                    return QualifiedNameParserSink.GetQualifiedName(this.Line, this.Col);

                if (this.Reason == ParseReason.MethodTip || 
                    this.Reason == ParseReason.MemberSelect || 
                    this.Reason == ParseReason.MemberSelectAndHighlightBraces)
                    return QualifiedNameParserSink.GetLastQualifiedName(this.Line, this.Col);

                return null;
            }
        }

        /// <summary>
        /// Gets the <see cref="QualifiedNameParserSink"/> associated with this <see cref="LuaParseRequest"/>.
        /// </summary>
        public QualifiedNameParserSink QualifiedNameParserSink
        {
            get { return Sink.FindParserSink<QualifiedNameParserSink>(); }
        }

        /// <summary>
        /// Specifies an <see cref="T:Microsoft.VisualStudio.Package.AuthoringSink"/> object used to contain information from the parsing operation.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// Returns an <see cref="T:Microsoft.VisualStudio.Package.AuthoringSink"/> object that was created in the <see cref="T:Microsoft.VisualStudio.Package.ParseRequest"/> class constructor.
        /// </returns>
        public new LuaAuthoringSink Sink
        {
            get { return (LuaAuthoringSink)base.Sink; }
        }

        /// <summary>
        /// Gets a value indicating whether a MemberSelect parse request was triggered by method call syntax.
        /// </summary>
        public bool IsMethodSelect
        {
            get
            {

                return this.TokenInfo.Token == (int) Tokens.COLON;
            }
        }
    }
}
